var CodeStudio = {
	Ajax: {
		getUrl: function(action, name) {
			return 'ajax.php?action=' + action + '&name=' + name;
		}
	},
	Tabs: {
		container: '#ui-split-main',
		
		getIndex: function(id) {
			var index = 0, selected = 0;
			$(CodeStudio.Tabs.container+' ul li a').each(function(){
				if ($(this).attr('href') == '#' + id) {
					selected = index;
				}
				++index;
			});
			return selected;
		},
		add: function(id, name) {
			$(CodeStudio.Tabs.container).tabs('add', '#'+id, name);
		},
		remove: function(id) {
			var index = CodeStudio.Tabs.getIndex(id);
			$(CodeStudio.Tabs.container).tabs('remove', index);
		}
	},
	File: {
		open: function(file, data) {
			var fileId = CodeStudio.File._idFromPath(file);
			var fileEditorId = fileId + '-' + Math.round(Math.random()*1000000);
			var fileName = CodeStudio.File._nameFromPath(file);
			var fileType = CodeStudio.File._typeFromName(fileName);
			
			// If the file is open already, switch to it
			var fileOpen = $('#'+fileId).length;
			if (fileOpen) {
				$('#ui-split-main').tabs('select', fileId);
				return;
			}
			
			// If the file is editable
			if (fileType != 'display') {
				$('#ui-split-main').append($('<div id="'+fileId+'"><textarea id="' + fileEditorId + '"></textarea></div>'));				
				$.getJSON(CodeStudio.Ajax.getUrl('getFile', file), function(data){
					$('#'+fileEditorId).val(data.data);
					var editor = editAreaLoader.init({
						id: fileEditorId	// id of the textarea to transform
						,start_highlight: true	// if start with highlight
						,allow_resize: "none"
						,allow_toggle: false
						,language: "en"
						,syntax: fileType
					});
					
					// Do frame wrapping functions
					var catchIt = setInterval(function(){
						var frame = $('#'+fileId+' iframe');
						if (frame.length) {
							
							frame.contents().find('head').append('<style>@import "/new/stylesheets/editor.css";</style>');
							frame.contents().bind('keydown', 'Ctrl+s', function(){
								CodeStudio.File.save(file, fileEditorId); return false;
							});
							
							clearInterval(catchIt);
						}
					}, 100);
				});
			} else {
				$('#ui-split-main').append($('<div id="'+fileId+'"><iframe src="'+CodeStudio.Ajax.getUrl('serveFile', file)+'" frameborder="0"></iframe></div>'));
			}
			
			// Add tab
			CodeStudio.Tabs.add(fileId, 
				'<span id="tab-'+fileId+'-status" class=""></span>' + 
				'<span>' + fileName + '</span>' +
				'<span id="tab-'+fileId+'-close" class="ui-icon ui-icon-close" onclick="CodeStudio.File.close(\''+fileId+'\');return false;">X</span>');
		},
		save: function(file, editor) {
			var fileId = CodeStudio.File._idFromPath(file);
			if(fileId == CodeStudio.File._getOpen()) {
				var value = editAreaLoader.getValue(editor);
				CodeStudio.File.setStatus(fileId, 'signal-diag');
				
				$.post(window.location + '/' + CodeStudio.Ajax.getUrl('saveFile', file), {data:value}, function(){
					CodeStudio.File.setStatus(fileId);
				});
			}
		},
		close: function(fileId) {
			CodeStudio.Tabs.remove(fileId);
			$('#'+fileId).remove();
		},
		
		_nameFromPath: function(path) {
			var split = path.split('/');
			return split[split.length-1];
		},
		_idFromPath: function(path) {
			return path.replace(/[\.\/]/g, '-');
		},
		
		// Tab functions
		_getOpen: function(path) {
			return $('li.ui-tabs-selected a').attr('href').substr(1);
		},
		setStatus: function(fileId, status) {
			var tab = $('#tab-'+fileId+'-status');
			if (typeof status != 'undefined') {
				tab.addClass('ui-icon');
				tab.addClass('ui-icon-'+status);
			} else {
				tab.attr('class', '');
			}
		},
		
		// Editor functions
		_typeFromName: function(name) {
			var split = name.split('.');
			var ext = split[split.length-1];
			
			var type;
			switch (ext) {
				// Normal -- don't change
				case 'c':
				case 'cpp':
				case 'css':
				case 'html':
				case 'js':
				case 'php':
				case 'sql':
				case 'vb':
				case 'xml':
				case 'txt':
					type = ext;
					break;
					
				// Modify
				case 'cf':
					type = 'coldfusion';
					break;
				case 'pl':
					type = 'perl';
					break;
				case 'py':
					type = 'python';
					break;
				case 'rb':
					type = 'ruby';
					break;
					
				// compatibility
				case 'php4':
				case 'php5':
				case 'phtml':
					type = 'php';
					break;
					
				default:
					type = 'display';
					break;
			}
			
			return type;
		}
	}
};